package com.mengxinya.ys.social.wechatplatform.handlers;

import com.mengxinya.ys.config.AppConfigService;
import com.mengxinya.ys.sdk.ServerManager;
import com.mengxinya.ys.sdk.ServerSdkException;
import com.mengxinya.ys.social.wechatplatform.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class WechatPlatformHandlerProviderImpl implements WechatPlatformHandlerProvider {
    private final ServerManager serverManager;
    private final AppConfigService appConfigService;

    public WechatPlatformHandlerProviderImpl(ServerManager serverManager, AppConfigService appConfigService) {
        this.serverManager = serverManager;
        this.appConfigService = appConfigService;
    }

    @Override
    public List<WechatPlatformHandler> getHandlers() {
        WechatPlatformHandler simpleHandler = new WechatPlatformHandler() {
            @Override
            public WechatMpReplyMessage handle(WechatMpMessage message) {
                return WechatMpReplyMessage.TEXT("收到：" + message.getContent());
            }

            @Override
            public WechatPlatformHandlerRule rule() {
                return new WechatPlatformHandlerRule() {
                    @Override
                    public String getContent() {
                        return "你好";
                    }
                };
            }
        };

        // 启动梯子
        WechatPlatformHandler startServerHandler = new WechatPlatformHandler() {
            @Override
            public WechatMpReplyMessage handle(WechatMpMessage message) {
                try {
                    serverManager.startServerInstance(appConfigService.getTiziServerDetail());
                    return WechatMpReplyMessage.TEXT("启动成功");
                } catch (ServerSdkException e) {
                    log.error(e.getLocalizedMessage(), e);
                    return WechatMpReplyMessage.TEXT("启动失败: " + e.getMessage());
                }
            }

            @Override
            public WechatPlatformHandlerRule rule() {
                return new WechatPlatformHandlerRule() {
                    @Override
                    public String getContent() {
                        return "启动梯子";
                    }
                };
            }
        };

        // 关闭梯子
        WechatPlatformHandler stopServerHandler = new WechatPlatformHandler() {
            @Override
            public WechatMpReplyMessage handle(WechatMpMessage message) {
                try {
                    serverManager.stopServerInstance(appConfigService.getTiziServerDetail());
                    return WechatMpReplyMessage.TEXT("关闭成功");
                } catch (ServerSdkException e) {
                    log.error(e.getLocalizedMessage(), e);
                    return WechatMpReplyMessage.TEXT("关闭失败: " + e.getMessage());
                }
            }

            @Override
            public WechatPlatformHandlerRule rule() {
                return new WechatPlatformHandlerRule() {
                    @Override
                    public String getContent() {
                        return "关闭梯子";
                    }
                };
            }
        };

        return List.of(simpleHandler);
    }
}
